#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/select.h>

#define ERROR_HANDLE(err) \
    if(err < 0){ \
        perror("error"); \
        exit(1); \
    }

// 添加客户端信息结构
struct client_info {
    struct sockaddr_in addr;
    int is_connected;
};

void *client_thread(void *arg){
    int clientfd = *(int *)arg;
    char buffer[1024] = {0};
    int count;
    while((count = recv(clientfd, buffer, sizeof(buffer), 0)) > 0){
        printf("count = %d, buffer = %s\n", count, buffer);
        send(clientfd, buffer, count, 0);
    }
    printf("clientfd = %d close\n", clientfd);
    close(clientfd);
}

int main(){
    
    int err;
    int socked = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(8888);

    ERROR_HANDLE(bind(socked, (struct sockaddr *)&server_addr, sizeof(server_addr)));

    listen(socked, 10);
    printf("listen socked = %d\n", socked);

    int maxfd = socked;
    fd_set masterfds, readfds;
    FD_ZERO(&masterfds);
    FD_SET(socked, &masterfds);
    int nready;
    
    // 添加客户端信息数组，假设最大支持1024个客户端
    struct client_info clients[1024];
    memset(clients, 0, sizeof(clients));

    while(1){
        readfds = masterfds;

        int i=socked;
        printf("before select readfds contain{");
        for(; i <= maxfd; i++){
            printf("%d,", i);
        }
        printf("}\n");

        nready = select(maxfd+1, &readfds, NULL, NULL, NULL);
        printf("after select readfds contain{");
        for(i = socked; i <= maxfd; i++){  // 重置i变量
            if(FD_ISSET(i, &readfds)){
                printf("%d,", i);
            }
        }
        printf("}\n");

        if(FD_ISSET(socked, &readfds)){
            struct sockaddr_in client_addr;
            socklen_t client_addr_len = sizeof(client_addr);
            int clientfd = accept(socked, (struct sockaddr *)&client_addr, &client_addr_len);
            printf("clientip = %s, clientport = %d, clientfd = %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), clientfd);
            FD_SET(clientfd, &masterfds);
            
            // 保存客户端地址信息
            if(clientfd < 1024){
                clients[clientfd].addr = client_addr;
                clients[clientfd].is_connected = 1;
            }
            
            if(clientfd > maxfd){
                maxfd = clientfd;
            }
        }
        
        for(i = socked + 1; i <= maxfd; i++){
            if(FD_ISSET(i,&readfds)){
                char buffer[10];
                memset(buffer, 0, sizeof(buffer));
                int ret = recv(i, buffer, sizeof(buffer), 0);
                if(ret <= 0){
                    // 使用保存的客户端地址信息
                    if(i < 1024 && clients[i].is_connected){
                        printf("clientip = %s, clientport = %d disconnect\n", 
                               inet_ntoa(clients[i].addr.sin_addr), 
                               ntohs(clients[i].addr.sin_port));
                        clients[i].is_connected = 0;  // 标记为断开
                    } else {
                        printf("client fd = %d disconnect\n", i);
                    }
                    FD_CLR(i, &masterfds);
                    close(i);
                }else{
                    printf("buffer:%s\n",buffer);
                    send(i, buffer, ret, 0);  // 回显数据
                }
            }
        }
        printf("\n");
    }

    close(socked);
    return 0;
}